//
//  YCRepresentResultController.m
//  ttttt
//
//  Created by itclimb on 07/06/2017.
//  Copyright © 2017 itclimb.yuancheng.com. All rights reserved.
//

#import "YCRepresentResultController.h"
#import "YCRepresentResultCell.h"
#import "YCRepresentModel.h"

@interface YCRepresentResultController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *representModels;
@property(nonatomic, assign) NSInteger page;

@end

@implementation YCRepresentResultController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的代言成果";
    [self.tableView registerNib:[UINib nibWithNibName:@"YCRepresentResultCell" bundle:nil]  forCellReuseIdentifier:@"RepresentCellId"];
    [self.view addSubview:self.tableView];
    
    self.page = 1;
    [self requestData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.representModels removeAllObjects];
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self requestData];
    }];
    
}

//MARK: - lazy load
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}
- (NSMutableArray *)representModels{
    if (!_representModels) {
        _representModels = [[NSMutableArray alloc] init];
    }
    return _representModels;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.representModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YCRepresentResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RepresentCellId"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    YCRepresentModel *model = self.representModels[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//MARK: - 获取代言成果数据
- (void)requestData{
    [self presentLoadingTips:@"正在加载中"];
    NSString *url = threeSaleListUrl;
    NSDictionary *params = @{
                             @"page_num":@(self.page),
//                             @"m_token_id":@"19",
                             @"link_mobile":[YCUserTool sharedTools].linkMobile//@"13263266381"
                             };
    
    __weak __typeof__(self)weakSelf = self;
    [DownloadData post:url params:params success:^(id json) {
        if (json) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            [self dismissTips];
            
            NSArray *jsonArr = (NSArray *)json;
            if (jsonArr.count == 0 && self.page == 1) {
                [self noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
            }
            
            [weakSelf.representModels addObjectsFromArray:[YCRepresentModel mj_objectArrayWithKeyValuesArray:json]];
            [weakSelf.tableView reloadData];
        }

    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self dismissTips];
    }];
}

@end
